<script>
import { pageMixin } from '@/mixins/index'
import { stateOptions } from '@/dicts/index'
import { getEnergyEfficiencyWarningApi, delWarningApi } from '@/api/footprint'
import addOrUpdate from './add_or_update.vue'
export default {
  name: 'EarlyWarning',
  components: { addOrUpdate },
  mixins: [pageMixin],
  data() {
    return {
      tableData: [],
      loading: false,
      form: {
        name: '',
        address: '',
        state: ''
      }
    }
  },
  computed: {
    getStateOptions() {
      return stateOptions
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    /**
     * 获取数据
     */
    getTableData() {
      this.loading = true
      getEnergyEfficiencyWarningApi(Object.assign({}, this.form, this.formPage)).then(res => {
        const { data, page, size, total } = res.data
        this.total = total
        this.formPage.page = page
        this.formPage.size = size
        this.tableData = data
        this.loading = false
      })
    },
    /**
     * 新增
     */
    addedHandle() {
      this.$refs.popupRef.init()
    },
    /**
     * 修改
     * @param {Object} data
     */
    modifyHandle(data) {
      this.$refs.popupRef.init(data)
    },
    /**
     * 删除
     * @param {number} id
     */
    deleteHandle(id) {
      this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delWarningApi(id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getTableData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 重置
     */
    rest(formName) {
      console.log(this.$refs[formName])
      this.$refs[formName].resetFields()
      this.getTableData()
    }
  }
}
</script>
<template>
  <el-card>
    <el-form ref="searchRef" :model="form" inline label-width="90px">
      <el-form-item label="检测点名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="检测点地址" prop="address">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="form.state">
          <el-option v-for="item in getStateOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <template>
          <el-button @click="rest('searchRef')">重置</el-button>
          <el-button type="primary" @click="getTableData">搜索</el-button>
          <el-button type="primary" @click="addedHandle">新增</el-button>
        </template>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData" :border="true">
      <el-table-column label="编号" prop="id" />
      <el-table-column label="检测点名称" prop="name" />
      <el-table-column label="检测点地址" prop="address" />
      <el-table-column label="实时功率" prop="power" />
      <el-table-column label="实时电压" prop="voltage" />
      <el-table-column label="状态" prop="state" />
      <el-table-column label="上报时间" prop="reportTime" width="160" />
      <el-table-column label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" fixed="right" width="150">
        <template v-slot="scope">
          <el-button type="success" size="small" plain round @click="modifyHandle(scope.row)">修改</el-button>
          <el-button type="danger" size="small" plain round @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="formPage.page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="formPage.size"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <add-or-update ref="popupRef" @updateData="getTableData" />
  </el-card>
</template>
